#include "first.h"
#include <QPainter>
#include "mypushbutton.h"
#include <QDebug>
#include <QTimer>
#include "hero.h"
#include <QKeyEvent>
#include <QTimer>

int array[120][80];

First::First(QWidget *parent) : QMainWindow(parent)
{
    this->setFixedSize(1200,800);

    this->setWindowIcon(QPixmap(":/pictures/title.png"));

    this->setWindowTitle("逃脱");

    MyPushButton * backBtn = new MyPushButton(":/pictures/backbutton.png");
    backBtn->setParent(this);
    backBtn->move(this->width()-backBtn->width(),this->height()-backBtn->height());

    connect(backBtn,&MyPushButton::clicked,[=](){

        backBtn->zoom1();
        backBtn->zoom2();

        QTimer::singleShot(500,this,[=](){
            emit this->ftb();
        });
    });

    m1 = new Question1;

    m2 = new Question2;

    m3 = new Question3;

    m4 = new Question4;

    connect(m1,&Question1::q1b,this,[=](){

        backBtn->zoom1();
        backBtn->zoom2();

        QTimer::singleShot(500,this,[=](){
            m1->hide();
            this->show();
        });
    });

    connect(m2,&Question2::q2b,this,[=](){

        backBtn->zoom1();
        backBtn->zoom2();

        QTimer::singleShot(500,this,[=](){
            m2->hide();
            this->show();
        });
    });

    connect(m3,&Question3::q3b,this,[=](){

        backBtn->zoom1();
        backBtn->zoom2();

        QTimer::singleShot(500,this,[=](){
            m3->hide();
            this->show();
        });
    });

    connect(m4,&Question4::q4b,this,[=](){

        backBtn->zoom1();
        backBtn->zoom2();

        QTimer::singleShot(500,this,[=](){
            m4->hide();
            this->show();
        });
    });

    fail = new Faliure;

    Hero ho;

}

void First::paintEvent(QPaintEvent *)
{
    QPainter painter(this);
    QPixmap pix;
    pix.load(":/pictures/backward.png");
    painter.drawPixmap(0,0,this->width(),this->height(),pix);
    pix.load(":/pictures/road.png");
    painter.drawPixmap(0,350,110,450,pix);
    painter.drawPixmap(110,660,260,140,pix);
    painter.drawPixmap(370,0,110,800,pix);
    painter.drawPixmap(480,0,250,140,pix);
    painter.drawPixmap(480,400,250,140,pix);
    painter.drawPixmap(730,0,110,800,pix);
    painter.drawPixmap(840,660,250,140,pix);
    painter.drawPixmap(1090,0,110,800,pix);
    pix.load(":/pictures/monster1.png");
    painter.drawPixmap(560,400,110,140,pix);
    pix.load(":/pictures/monster2.png");
    painter.drawPixmap(560,0,110,140,pix);
    pix.load(":/pictures/jiemi.png");
    painter.drawPixmap(425,730,55,70,pix);
    painter.drawPixmap(785,470,55,70,pix);
    painter.drawPixmap(1145,470,55,70,pix);
    pix.load(":/pictures/exit.png");
    painter.drawPixmap(1145,0,55,70,pix);

    for(int i=0;i<11;i++)
    {
        for(int j=35;j<80;j++)
        {
            array[i][j]=1;
        }
    }

    for(int i=11;i<37;i++)
    {
        for(int j=66;j<80;j++)
        {
            array[i][j]=1;
        }
    }

    for(int i=48;i<73;i++)
    {
        for(int j=0;j<14;j++)
        {
            array[i][j]=1;
        }
        for(int j=40;j<54;j++)
        {
            array[i][j]=1;
        }
    }

    for(int j=0;j<80;j++)
    {
        for(int i=37;i<48;i++)
        {
            array[i][j]=1;
        }
        for(int i=73;i<84;i++)
        {
            array[i][j]=1;
        }
        for(int i=109;i<120;i++)
        {
            array[i][j]=1;
        }
    }

    for(int i=84;i<109;i++)
    {
        for(int j=66;j<80;j++)
        {
            array[i][j]=1;
        }
    }

    for(int i=56;i<67;i++)
    {
        for(int j=40;j<54;j++)
        {
            array[i][j]=2;
        }
    }

    for(int i=42;i<48;i++)
    {
        for(int j=73;j<80;j++)
        {
            array[i][j]=3;
        }
    }

    for(int i=78;i<84;i++)
    {
        for(int j=47;j<54;j++)
        {
            array[i][j]=3;
        }
    }

    for(int i=114;i<120;i++)
    {
        for(int j=47;j<54;j++)
        {
            array[i][j]=3;
        }
    }

    for(int i=114;i<120;i++)
    {
        for(int j=0;j<7;j++)
        {
            array[i][j]=3;
        }
    }

    painter.drawPixmap(ho.h_x,ho.h_y,ho.hero);
}

void First::keyPressEvent(QKeyEvent * event)
{
    switch(event->key())
    {
    case Qt::Key_J:
    {
        if(((array[ho.h_x/10][ho.h_y/10]==3) | (array[(ho.h_x+ho.hero.width())/10][((ho.h_y+ho.hero.height())/10)]==3) | (array[(ho.h_x+ho.hero.width())/10][ho.h_y/10]==3)) & ho.h_x<500)
        {
            this->hide();
            m1->show();
        }
        else if(((array[ho.h_x/10][ho.h_y/10]==3) | (array[(ho.h_x+ho.hero.width())/10][((ho.h_y+ho.hero.height())/10)]==3) | (array[(ho.h_x+ho.hero.width())/10][ho.h_y/10]==3)) & ho.h_x<1000)
        {
            this->hide();
            m2->show();
        }
        else if(((array[ho.h_x/10][ho.h_y/10]==3) | (array[(ho.h_x+ho.hero.width())/10][((ho.h_y+ho.hero.height())/10)]==3) | (array[(ho.h_x+ho.hero.width())/10][ho.h_y/10]==3)) & ho.h_y>300)
        {
            this->hide();
            m3->show();
        }
        else if((array[ho.h_x/10][ho.h_y/10]==3) | (array[(ho.h_x+ho.hero.width())/10][((ho.h_y+ho.hero.height())/10)]==3) | (array[(ho.h_x+ho.hero.width())/10][ho.h_y/10]==3))
        {
            this->hide();
            m4->show();
        }
        else
            break;
    }
    case Qt::Key_W:
    {
        int i,j;
        if((array[ho.h_x/10][ho.h_y/10-1]==0) | (array[(ho.h_x+ho.hero.width())/10][((ho.h_y+ho.hero.height())/10)-1]==0))
        {
            i=ho.h_x;
            j=ho.h_y;
        }
        else if((array[ho.h_x/10][ho.h_y/10-1]==2) | (array[(ho.h_x+ho.hero.width())/10][((ho.h_y+ho.hero.height())/10)-1]==2))
        {
            this->hide();
            fail->show();
        }
        else
        {
            if(ho.h_y<10)
            {
                i = ho.h_x;
                j = 0;
            }
            else
            {
              i = ho.h_x;
              j = ho.h_y-10;
            }
        }
        ho.setPosition(i,j);
        update();
        break;
    }
    case Qt::Key_S:
    {
        int i,j;
        if((array[ho.h_x/10][ho.h_y/10+1]==0) | (array[(ho.h_x+ho.hero.width())/10][((ho.h_y+ho.hero.height())/10)+1]==0))
        {
            i=ho.h_x;
            j=ho.h_y;
        }
        else if((array[ho.h_x/10][ho.h_y/10+1]==2) | (array[(ho.h_x+ho.hero.width())/10][((ho.h_y+ho.hero.height())/10)+1]==2))
        {
            this->hide();
            fail->show();
        }
        else
        {
            if(ho.h_y+ho.hero.height()>790)
            {
                i = ho.h_x;
                j = 800-ho.hero.height();
            }
            else
            {
                i = ho.h_x;
                j = ho.h_y+10;
            }
        }
        ho.setPosition(i,j);
        update();
        break;
    }
    case Qt::Key_A:
    {
        int i,j;
        if((array[ho.h_x/10-1][ho.h_y/10]==0) | (array[(ho.h_x+ho.hero.width())/10-1][((ho.h_y+ho.hero.height())/10)]==0))
        {
            i=ho.h_x;
            j=ho.h_y;
        }
        else if((array[ho.h_x/10-1][ho.h_y/10]==2) | (array[(ho.h_x+ho.hero.width())/10-1][((ho.h_y+ho.hero.height())/10)]==2))
        {
            this->hide();
            fail->show();
        }
        else
        {
            if(ho.h_x<10)
            {
                i = 0;
                j = ho.h_y;

            }
            else
            {
                i = ho.h_x-10;
                j = ho.h_y;
            }
        }
        ho.setPosition(i,j);
        update();
        break;
    }
    case Qt::Key_D:
    {
        int i,j;
        if((array[ho.h_x/10+1][ho.h_y/10]==0) | (array[(ho.h_x+ho.hero.width())/10+1][((ho.h_y+ho.hero.height())/10)]==0))
        {
            i=ho.h_x;
            j=ho.h_y;
        }
        else if((array[ho.h_x/10+1][ho.h_y/10]==2) | (array[(ho.h_x+ho.hero.width())/10+1][((ho.h_y+ho.hero.height())/10)]==2))
        {
            this->hide();
            fail->show();
        }
        else
        {
            if(ho.h_x+ho.hero.width()>1190)
            {
                i = 1200-ho.hero.width();
                j = ho.h_y;

            }
            else
            {
                i = ho.h_x+10;
                j = ho.h_y;
            }
        }
        ho.setPosition(i,j);
        update();
        break;
    }
    }
}



